home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 18000 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.9 KB

  1. Path: news.gate.net!rdl-ilps-rxh2
  2. From: rmcinnis@gate.net (Robert B McInnis)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: File I/O using fwrite()
  5. Date: 18 Apr 1996 13:27:14 GMT
  6. Organization: 9th Bit Software
  7. Message-ID: <4l5fvi$295a@news.gate.net>
  8. References: <31758D70.1D38@vixa.voyager.net> <4l4s79$r1v@nadine.teleport.com>
  9. NNTP-Posting-Host: miafl2-50.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. >David Wade <dwade@vixa.voyager.net> wrote:
  13. >HELP!!!  I am having a major problem getting random access file routines 
  14. >to work properly.  For some reason, no matter what I do the information 
  15. >is always written at the end of the file instead of where I want it to 
  16. >replace...  All I need to do is update a record in a potentailly large 
  17. >file, and I can't load the entire file and re-write it to do it.  If 
  18. >anyone sees what I am doing wrong, or has a possible solution using 
  19. >other means, I would greatly appreciate the info.  The funtion looks 
  20. >like this:
  21. >        f=_fsopen(".\\data\\weapons.dat", "ab+", SH_DENYNONE);
  22. >        if(f == NULL)
  23. >        {
  24. >[snip]
  25. >        };
  26. >        rewind(f);
  27. >        fseek(f,(item_data.reference-1)*sizeof(item_struct),SEEK_SET);
  28. >        fwrite(&item_data, sizeof(item_struct), 1, f);
  29. >        fclose(f);
  30. >
  31. >
  32. >The struct is of type item_struct, and item_data is an instance of that 
  33. >struct.
  34. >
  35.  
  36. Try taking the append command out of the mode string.  For random access 
  37. binary files, use this mode string : "w+b" (order doesn't matter)
  38.  
  39. Check for file existence and create it if you have to.  
  40.  
  41. Don't bother doing rewind.  Just fseek with a SEEK_SET and a long offset.  
  42.  
  43. If you'd like a SMALL set of classes that does most of this for you, just ask.  
  44. I also have a descendent class that works for Ms Windows specifically, 
  45. otherwise it's just C++.
  46.  
  47. Hope this helps,
  48.  
  49. Rob
  50.  
  51. ----------------------------
  52. Robert McInnis
  53. 9th Bit Software
  54. (eMail)  rmcinnis@gate.net
  55. (web  )  http://www.gate.net/~rmcinnis
  56.  
  57.  
  58.  
  59.  
  60.